<?php
	/**
	 * 
	 */
	class NodeAction extends CommonAction {
		
		public function index()
		{
			$node  = M("Node");
			$where = array();
			if (isset($_GET['pid']) && !empty($_GET['pid'])){
				$where['pid'] = (int)$_GET['pid'];
			}else{
				$where['pid'] = 0;
			}
			
	        $list = $node->where( $where )->order("sort")->select();
			$this->assign("list",$list);
			Cookie::set ( '_currentUrl_', __SELF__ );
			$this->display();
		}
		
		public function add()
		{
			$this->display();
		}
		
		public function edit()
		{
			$model = M ( 'Node' );
			//$id = $_REQUEST [$model->getPk ()];
			$vo = $model->getById ( $_GET['id'] );
			$this->assign ( 'vo', $vo );
			$this->display ();
		}
		
		public function insert()
		{
			$model = D ('Node');
			if (false === $model->create ()) {
				$this->error ( $model->getError () );
			}
			//保存当前数据对象
			$list=$model->add ();
			if ($list!==false) { //保存成功
				//header("location:".$_SERVER['HTTP_REFERER']);
				if ($_POST['pid'] != 0){
					$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
				}else{
					$this->assign ( 'jumpUrl', "__URL__" );
				}
				$this->success ('新增成功!');
			} else {
				//失败提示
				$this->assign ( 'jumpUrl', "__URL__" );
				$this->error ('新增失败!');
			}
		}
		
		public function delete()
		{
			$model = M ('Node');
			
			if ($model->where ( "id='".$_GET['id']."'" )->setField ( 'status', -1 )){
				//@header("location:".$_SERVER['HTTP_REFERER']);
				$this->assign('jumpUrl',$_SERVER['HTTP_REFERER']);
				$this->success ('删除完成！');
			}else{
				//$this->assign ( 'jumpUrl', "__URL__" );
				$this->error ('删除失败！');
			}
		}
		
		public function resume()
		{
			$model = D ('Node');
			if (false !== $model->resume ( "id='".$_GET['id']."'" )) {
				@header("location:".$_SERVER['HTTP_REFERER']);
			} else {
				//$this->assign ( 'jumpUrl', "__URL__" );
				$this->error ( '状态恢复失败！' );
			}
		}
		
		public function forbid() {
			$model = D ('Node');
			$list = $model->forbid ( "id='".$_GET['id']."'" );
			if ($list !== false) {
				@header("location:".$_SERVER['HTTP_REFERER']);
			} else {
				$this->assign ( 'jumpUrl', "__URL__" );
				$this->error  (  '状态禁用失败！' );
			}
		}
		
		public function recycle() {
			$model = D ('Node');
			if (false !== $model->recycle ( "id='".$_GET['id']."'" )) {
				@header("location:".$_SERVER['HTTP_REFERER']);
			} else {
				$this->assign ( 'jumpUrl', "__URL__" );
				$this->error   (  '状态还原失败！' );
			}
		}
		
		public function checkDelete()
		{
			$model = M('Node');
			$data = $model->where("pid='".$_GET['id']."'")->select();
			if (!empty($data)){
				$this->ajaxReturn('','存在子级模型，需先删除子级模型！',0);
			}else{
				$this->ajaxReturn('','',1);
			}
		}
		
		public function _before_edit() {
			$this->getGroupOption($_GET['gid']);
		}
		
		public function _before_delete()
		{
			
		}
		
		public function _before_index() {
			$model	=	M("Group");
			$list	=	$model->where('status=1')->getField('id,title');
			$this->assign('groupList',$list);
		}
		
		// 获取配置类型
		public function _before_add() {
			$this->getGroupOption();
			
			$node	=	M("Node");
			$node->getById($_GET['pid']);
	        $this->assign('pid',$node->id);
			$this->assign('level',$node->level+1);
		}
		
		private function getGroupOption($gid=''){
			$model	=	M("Group");
			$list	=	$model->where('status=1')->field('id,pid,name,title')->select();
			
			//倒入树形类
			import('ORG.Util.Trees');
			$tree = new tree($list);
			$option = '';
			$str = "<option value=\$id \$selected>\$spacer\$title</option>";
			if (!empty($gid)){
				$option .= $tree->getTreelist(0,$str,$gid);
			}else{
				$option .= $tree->getTreelist(0,$str);
			}
			
			$this->assign('option',$option);
		}
		
	}
	
?>